package com.boc.ewr.business.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boc.ewr.business.entity.EwrQuarterlyReport;
import com.boc.ewr.business.service.IEwrQuarterlyReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.Set;

/**
 * @Description: 季度报告
 * @Author: jeecg-boot
 * @Date:   2023-05-04
 * @Version: V1.0
 */
@Api(tags="季度报告")
@RestController
@RequestMapping("/business/ewrQuarterlyReport")
@Slf4j
public class EwrQuarterlyReportController extends JeecgController<EwrQuarterlyReport, IEwrQuarterlyReportService> {
	@Autowired
	private IEwrQuarterlyReportService ewrQuarterlyReportService;
	 @Autowired
	 private ISysBaseAPI sysBaseAPI;
	/**
	 * 分页列表查询
	 *
	 * @param ewrQuarterlyReport
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "季度报告-分页列表查询")
	@ApiOperation(value="季度报告-分页列表查询", notes="季度报告-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<EwrQuarterlyReport>> queryPageList(EwrQuarterlyReport ewrQuarterlyReport,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<EwrQuarterlyReport> queryWrapper = QueryGenerator.initQueryWrapper(ewrQuarterlyReport, req.getParameterMap());
		Set<String> orgCodeSetByLoginUser = sysBaseAPI.getOrgCodeSetByLoginUser(this.getLoginUser());
		if (orgCodeSetByLoginUser!=null) {
			queryWrapper.in("qr_branch_no", orgCodeSetByLoginUser);
		}
		Page<EwrQuarterlyReport> page = new Page<EwrQuarterlyReport>(pageNo, pageSize);
		IPage<EwrQuarterlyReport> pageList = ewrQuarterlyReportService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param ewrQuarterlyReport
	 * @return
	 */
	@AutoLog(value = "季度报告-添加")
	@ApiOperation(value="季度报告-添加", notes="季度报告-添加")
	//@RequiresPermissions("org.jeecg.modules:ewr_quarterly_report:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody EwrQuarterlyReport ewrQuarterlyReport) {
		//计算是否逾期-根据所属季度，反显是否逾期

		//添加创建人和创建时间
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		ewrQuarterlyReport.setCreateName(user.getRealname());
		ewrQuarterlyReport.setCreateTime(new Date());
		ewrQuarterlyReportService.save(ewrQuarterlyReport);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param ewrQuarterlyReport
	 * @return
	 */
	@AutoLog(value = "季度报告-编辑")
	@ApiOperation(value="季度报告-编辑", notes="季度报告-编辑")
	//@RequiresPermissions("org.jeecg.modules:ewr_quarterly_report:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody EwrQuarterlyReport ewrQuarterlyReport) {
		ewrQuarterlyReportService.updateById(ewrQuarterlyReport);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "季度报告-通过id删除")
	@ApiOperation(value="季度报告-通过id删除", notes="季度报告-通过id删除")
	//@RequiresPermissions("org.jeecg.modules:ewr_quarterly_report:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		ewrQuarterlyReportService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "季度报告-批量删除")
	@ApiOperation(value="季度报告-批量删除", notes="季度报告-批量删除")
	//@RequiresPermissions("org.jeecg.modules:ewr_quarterly_report:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.ewrQuarterlyReportService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "季度报告-通过id查询")
	@ApiOperation(value="季度报告-通过id查询", notes="季度报告-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<EwrQuarterlyReport> queryById(@RequestParam(name="id",required=true) String id) {
		EwrQuarterlyReport ewrQuarterlyReport = ewrQuarterlyReportService.getById(id);
		if(ewrQuarterlyReport==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(ewrQuarterlyReport);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param ewrQuarterlyReport
    */
    //@RequiresPermissions("org.jeecg.modules:ewr_quarterly_report:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, EwrQuarterlyReport ewrQuarterlyReport) {
        return super.exportXls(request, ewrQuarterlyReport, EwrQuarterlyReport.class, "季度报告");
    }

	 private LoginUser getLoginUser() {
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 return sysUser;
	 }

}
